<?php
/**
+--------------------------------------------------------------
 * User: maqun
+--------------------------------------------------------------
 * Date: 2015/12/5
+--------------------------------------------------------------
 * Time: 14:42
+--------------------------------------------------------------
 * 用户信息,登录信息,个人账户,做题统计等信息
+--------------------------------------------------------------
 */
class UserAction extends BaseAction{

    public function _initialize(){
        self::$action = -1;
        parent::_initialize();
    }
    /**
     * 显示用户做题信息
     */
    public function index(){

        //获取用户信息,姓名,学号,学校，邮箱，做题数量等
        $user_id = !empty($_GET['id']) ? $this->filterUserid($_GET['id']):$this->error('用户id不合法');

        $fields  = array('user_id','email','nick','school');
        $where   = "user_id='{$user_id}' and defunct='N' ";
        $user_info     = D('Users')->field($fields)->where($where)->limit(1)->find();

        if(empty($user_info)){
            $this->error('用户不存在');
        }
        $solution_info = D('Solution')->getUserSolution($user_info['user_id']);

//        dump($solution_info);exit();
        $this->assign('user',$user_info);
        $this->assign('solution',$solution_info);
        $this->display('index');
    }

    /**
     * 登录
     */
    public function login(){

        $user_id   = $this->_post('user_id');
        $password  = $this->_post('password');
        $obj       = $this->getInstance();
        $return = array('status'=>'','info'=>'');
        if(!empty($user_id) && !empty($password)){
            $info = $obj->checkLogin($user_id,$password);
            if(false!==$info){
                $_SESSION['user_id'] = $info;
                $return['status'] = true;
                $return['info'] = "登录成功";
            }else{
                $return['status'] = false;
                $return['info'] = "用户名或密码错误";
            }

        }else{
            $return['status'] = false;
            $return['info'] = "用户名或密码错误";
        }
        $this->ajaxReturn($return);
    }
    /**
     * 注销
     */
    public function logout(){
        session_destroy();
        redirect(U('Index/index'));
    }
    public function _before_editInfo(){
        if(empty($_SESSION['user_id'])){
            echo "login frist";
        }
    }
    /**
     * 编辑用户信息
     */
    public function editInfo(){

        if(empty($_POST)){
            $info = D('Users')->returnUser();
            if(false===$info){
                $this->error('请登录');
            }
            if(empty($info)){
                $this->error('用户不存在');
            }
            $this->assign('info',$info['user_info']);
            $this->assign('solution',$info['solution_info']);
            $this->display('edit');
        }else{
            $res = D('Users')->editProfile($_POST);
            if($res['status']){
                $this->success('修改成功');
            }else{
                $this->error($res['info']);
            }
        }
    }
    /**
     * @param string $type
     * @return Model
     * 实例化登录模型
     */
    public function getInstance($type='local'){
        $obj = new Model();
        switch($type){
            case 'local':
                $obj = D('Users');
                break;
            case 'moodle':
                $obj = D('Moodle');
                break;
            case 'discuz':
                $obj = D('Discuz');
                break;
            default:
                $obj = D('Users');
                break;
        }
        return $obj;
    }

    /**
     * 注册时异步判断user_id是否可注册
     */
    public function userExist(){
        $return_info = array('status'=>'','info'=>'');
        $user_id     = $this->filterUserid($_POST['user_id']);//过滤用户id是否合法

        if(empty($user_id)){
            $return_info['status'] = false;
            $return_info['info']   = "User ID is not legal";
        }else{
            $res = D('Users')->where('user_id='.$user_id)->select();
            if(!empty($res)){
                $return_info['status'] = false;
                $return_info['info']   = "User ID already exists";
            }else{
                $return_info['status'] = true;
                $return_info['info']   = "Allow";
            }
        }
        $this->ajaxReturn($return_info);
    }

    /**
     * 注册
     */
    public function register(){

        $res = D('Users')->register($_POST);

        if($res['status']){
            $_SESSION['user_id'] = $res['info'];
            $this->success('注册成功');
        }else{
            $this->error($res['info']);
        }
        exit;
    }

    /**
     * 用户修改资料时，异步判断输入的旧密码是否正确
     */
    public function checkOldPass(){

        $return  = array('status'=>true,'info'=>'Allow');
//        $old_pass = D('Users')->where("user_id='".$_SESSION['user_id']."'")->getField('password');

        $obj       = $this->getInstance();
        $info = $obj->checkLogin($_SESSION['user_id'],$_POST['old_pass']);
        if(false===$info){
            $return['status'] = false;
            $return['info']   = "Old Password Error";
        }
        $this->ajaxReturn($return);
    }
    //获取个人做题信息
    public function getPersonlData(){

        $user_id   = $this->_post('user_id');
        $solution_info = D('Solution')->getUserSolution($user_id);

        $solution_info = !empty($solution_info)?$solution_info['solution_info']:'';
        $return = array('status'=>'','info'=>'');
        if(!empty($solution_info)){
            $return ['status'] = true;
            foreach($solution_info as $k=>$v){

                $return['info'][$k]['name']    = $v['name'];
                $return['info'][$k]['y']       = intval($v['num']);
            }
        }
        $this->ajaxReturn($return);

    }
    /**
     * 获取统计数据,每天提交/AC数量 默认为所有数据
     */
    public function getStatistics(){

        $start     = $this->_post('s_date');
        $end       = $this->_post('e_date');
        $user_id   = $this->_post('user_id');
        $start = date('Y-m-d H:i:s',time()-100*24*60*60);
        $end   = date('Y-m-d H:i:s',time());
        if(empty($user_id)){
            $this->ajaxReturn(array('status'=>false,'info'=>'暂无数据'));
        }
        //统计数据
        $res = D('Solution')->getInfo('','',$user_id);
        if($res['status']){
            $this->ajaxReturn($res);
        }else{
            $this->ajaxReturn(array('status'=>false,'info'=>'暂无数据'));
        }
        exit();
    }
}